<template>
  <div class="container">
    <div class="screen-container">
      <!-- 搜索框 -->
      <search-form
        :data="queryParams"
        :field-list="filterInfo.fieldList"
        @handleSearch="handleQuery"
        @handleReset="resetQuery"
      />
    </div>
    <el-row :gutter="10">
      <el-button
        type="primary"
        icon="el-icon-plus"
        size="mini"
        @click="handleAdd"
      >新建</el-button>
      <el-button
        :disabled="checkNodes.length == 0"
        icon="el-icon-delete"
        size="mini"
        @click="handleDel"
      >删除</el-button>
    </el-row>
    <HkTable
      :loading="loading"
      :table-data="tableList"
      :table-columns="tableFieldList"
      :show-pag="true"
      :show-index="true"
      :query-params="queryParams"
      rowkey="id"
      :total="total"
      :operate="true"
      :selection="true"
      :select-fn="selectFn"
      table-height="80"
      :show-overflow-tooltip="true"
      @changePage="changePage"
      @handleSelectionChange="handleSelectionChange"
    >
      <template #operate="scope">
        <el-button
          v-if="scope.row.type == 2"
          type="text"
          @click="handleEdit(scope.row)"
        >编辑</el-button>
        <el-button type="text" @click="handleCopy(scope.row)">复制</el-button>
      </template>
      <template #name="{ row }">
        <el-button type="text" @click="handlePreview(row)">{{
          row.name
        }}</el-button>
      </template>
      <template #type="scope">
        <span>{{ scope.row.type == 1 ? "内置表单" : "用户自定义表单" }}</span>
      </template>
    </HkTable>
    <form-dialog ref="formDialogRef" :title="title" />
  </div>
</template>

<script>
import FormDialog from './formDialog.vue'
import SearchForm from '@/components/HkSearchform'
import HkTable from '@/components/HkTable'
import { deleteForm, getFormPageList } from '@/api/bpmn/formDesign.js'

export default {
  components: { SearchForm, HkTable, FormDialog },
  beforeRouteLeave(to, from, next) {
    from.meta.keepAlive = false
    next()
  },
  data() {
    return {
      // 新增框是否 可见
      open: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: '',
      // 查询参数
      queryParams: {
        size: 10,
        current: 1,
        name: ''
      },
      // 查询
      filterInfo: {
        fieldList: [{ label: '关键字', type: 'input', field: 'name' }]
      },
      tableList: [],
      tableFieldList: [
        { label: '名称', param: 'name', type: 'slot' },
        { label: '表单类型', param: 'type', type: 'slot', width: '200' },
        { label: '描述', param: 'description' }
      ],
      checkNodes: []
    }
  },
  mounted() {
    this.getList()
  },

  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      getFormPageList(this.queryParams).then((res) => {
        this.tableList = res.list
        this.total = res.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery(value) {
      this.queryParams.current = 1
      this.queryParams.name = value.name
      this.getList()
    },
    /** 重置按钮*/
    resetQuery() {
      this.queryParams = {
        name: undefined,
        size: 10,
        current: 1
      }
      this.getList()
    },
    /** 分页*/
    changePage(value) {
      this.getList()
    },
    /** 预览 */
    handlePreview(row) {
      this.title = `${row.name}预览`
      this.$refs.formDialogRef.show(row.id)
    },
    /** 新增 */
    handleAdd() {
      this.$router.push(window.prefix + '/bpmn/formDesign/create')
    },
    /**  编辑 */
    handleEdit(row) {
      this.$router.push({
        path: window.prefix + '/bpmn/formDesign/create',
        query: { type: 'edit', id: row.id }
      })
    },
    //  复制
    handleCopy(row) {
      this.$router.push({
        path: window.prefix + '/bpmn/formDesign/create',
        query: { type: 'copy', id: row.id }
      })
    },
    //  删除
    handleDel() {
      const _this = this
      this.$modal
        .confirm('是否确认删除所选数据项？')
        .then(function() {
          const ids = _this.checkNodes.map((i) => i.id).join(',')
          return deleteForm(ids)
        })
        .then(() => {
          this.$modal.msgSuccess('删除成功')
          this.getList()
        })
        .catch((error) => {
          console.log(error)
        })
    },

    //  多选框是否可用
    selectFn(row, index) {
      return !row.ref
    },
    // 触发多选框
    handleSelectionChange(val) {
      this.checkNodes = val
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep .el-form-item {
  margin-bottom: 0 !important;
}
.container {
  padding: 0 20px;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}

::v-deep .el-table .cell {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
}

.app-container {
  padding: 0;
}

.iconi {
  width: 16px;
  height: 16px;
  display: inline-block;
  text-align: center;
  border-radius: 50%;
  margin-right: 10px;
  line-height: 16px;
  color: white;

  &::before {
    font-size: 10px;
    font-weight: 600;
  }
}
</style>
